#include <algorithm>

#include "deepsort/nn_matching.h"

using namespace std;
using namespace cv;

namespace {

    float iou(const BBox &b1, const BBox &b2) {
        auto in = (b1.rect & b2.rect).area();
        auto un = b1.rect.area() + b2.rect.area() - in;

        if (un < DBL_EPSILON)
            return 0;

        return (float)in / un;
    }
}

vector<vector<float>> iou_dist(const vector<BBox> &dets, const vector<BBox> &trks) {
    auto trk_num = trks.size();
    auto det_num = dets.size();
    auto dist = vector<vector<float>>(int64_t(trk_num), vector<float>(int64_t(det_num)));
    for (size_t i = 0; i < trk_num; i++) // compute iou matrix as a distance matrix
    {
        for (size_t j = 0; j < det_num; j++) {
            dist[i][j] = 1 - iou(trks[i], dets[j]);
        }
    }
    return dist;
}
